
function changeModelColorLoading(self, tiyData) {
  tiyData.tStatus.modifyModal.isLoading = false;
  self.$nextTick(function () {
    tiyData.tStatus.modifyModal.isLoading = true;
  });
}

export const init = (self, tiyData) => {
  tiyData.tPage.currentPage = 1;
  tiyData.tPage.stepNumber = 40;
  tiyData.tParam.postageName = '';
  tiyData.tParam.searchParams.keyWord = '';
  tiyData.tStatus.modifyModal.isShow = false;
  tiyData.tStatus.decryptedModal.isShow = false;
}

export const webList = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.getTerminalMarks({
    pageNum:tiyData.tPage.currentPage,
    pageSize:tiyData.tPage.stepNumber,
    keyWord:tiyData.tParam.searchParams.keyWord
  }).then(function (response) {
      self.$Spin.hide();
      if (response.success) {
        tiyData.tParam.length = [];
        tiyData.tData.terminalMarkList.splice(0,tiyData.tData.terminalMarkList.length) ;
        let markPage = response.result;
        tiyData.tPage.total = markPage.total;
        tiyData.tPage.stepNumber = markPage.pageSize;
        if (markPage.list.length !==0){
          markPage.list.forEach(
            function (item) {
              tiyData.tData.terminalMarkList.push(item);
            }
          );
        }

      } else {
        self.$Spin.hide();
        self.$Message.error(response.errorMessage);
      }
    });
};

export const saveTerminalMark = (self, tiyData) => {
  self.$refs["terminalMarkForm"].validate((valid) => {
    if (valid) {
      self.$Spin.show();
      tiyData.tStatus.modifyModal.isLoading = true;
      self.$apiOther.addEditTerminalMark({
        id:tiyData.tParam.terminalMark.id,
        ip:tiyData.tParam.terminalMark.ip,
        borwerFinger:tiyData.tParam.terminalMark.borwerFinger,
        terminalType:tiyData.tParam.terminalMark.terminalType,
        borwerName:tiyData.tParam.terminalMark.borwerName,
        terminalNo:(tiyData.tParam.terminalMark.computeBy != "" ? tiyData.tParam.terminalMark.computeBy + "-" : "")  +  tiyData.tParam.terminalMark.terminalNo,
        remark:tiyData.tParam.terminalMark.remark,
        expireTime:tiyData.tParam.terminalMark.expireTime,
        effectiveTime:tiyData.tParam.terminalMark.effectiveTime,
        employeeAccount:tiyData.tParam.terminalMark.employeeAccount,
      }).then(function (response) {
        self.$Spin.hide();
        changeModelColorLoading(self, tiyData);
        if (response.success) {
          tiyData.tStatus.modifyModal.isShow=false;
          webList(self, tiyData);
          self.$Message.success('提交成功!');
        } else {
          tiyData.tStatus.modifyModal.isLoading = false;
          self.$Message.error(response.errorMessage);
        }
      });
    }else {
      changeModelColorLoading(self,tiyData);
    }
  });

};

export const modifyTerminalMark = (self, tiyData,index) => {
  self.$refs["terminalMarkForm"].resetFields();
  let selectedTerminalMark= tiyData.tData.terminalMarkList[index];
  tiyData.tParam.terminalMark.id = selectedTerminalMark.id;
  tiyData.tParam.terminalMark.ip = selectedTerminalMark.ip;
  tiyData.tParam.terminalMark.borwerFinger = selectedTerminalMark.borwerFinger;
  tiyData.tParam.terminalMark.borwerName = selectedTerminalMark.borwerName == "" ? "Chrome" : selectedTerminalMark.borwerName;
  tiyData.tParam.terminalMark.terminalNo = selectedTerminalMark.terminalNo;
  tiyData.tParam.terminalMark.computeBy = "";
  tiyData.tParam.terminalMark.terminalType = selectedTerminalMark.terminalType;
  tiyData.tParam.terminalMark.remark = selectedTerminalMark.remark;
  tiyData.tParam.terminalMark.effectiveTime = selectedTerminalMark.effectiveTime;
  tiyData.tParam.terminalMark.expireTime = selectedTerminalMark.expireTime;
  if (selectedTerminalMark.effectiveTime == -1 && selectedTerminalMark.expireTime == -1 ){
    tiyData.tParam.terminalMark.isLongTerm = true;
  }else {
    tiyData.tParam.terminalMark.isLongTerm = false;
  }
  tiyData.tParam.terminalMark.startEndTime.length = 0;
  if(selectedTerminalMark.effectiveTime > 0){
    tiyData.tParam.terminalMark.startEndTime.push(new Date(parseInt(selectedTerminalMark.effectiveTime + (new Date().getTimezoneOffset() * 1000 * 60))) );//转换为UTC时间显示
    tiyData.tParam.terminalMark.startEndTime.push(new Date(parseInt(selectedTerminalMark.expireTime + (new Date().getTimezoneOffset() * 1000 * 60))));
  }

  //tiyData.tParam.chooseTime = new Date(selectedTerminalMark.expireTime - new Date().getTimezoneOffset() * 60 * 1000);
  tiyData.tParam.terminalMark.employeeAccount = selectedTerminalMark.employeeAccount;
  tiyData.tStatus.modifyModal.isShow=true;
  tiyData.tStatus.modifyModal.title="修改电脑标记";
};


export const deleteTerminalMark = (self, tiyData,id) => {
  tiyData.tParam.deleteterminalMarkId = id;
  tiyData.tStatus.actionModal.isShow=true;
  tiyData.tStatus.actionModal.title="删除电脑标记";
};

export const webDeleteTerminalMark = (self, tiyData) => {
  self.$Spin.show();
  self.$apiOther.deleteTerminalMark({
    id:tiyData.tParam.deleteterminalMarkId
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      tiyData.tStatus.actionModal.isShow=false;
      webList(self, tiyData);
      self.$Message.success('提交成功!');
    } else {
      self.$Spin.hide();
      self.$Message.error(response.errorMessage);
    }
  });
};


